package com.kc.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.bus.domain.Goods;
import com.kc.bus.domain.Inport;
import com.kc.bus.domain.Outport;
import com.kc.bus.domain.Provider;
import com.kc.bus.mapper.GoodsMapper;
import com.kc.bus.mapper.InportMapper;
import com.kc.bus.mapper.OutportMapper;
import com.kc.bus.service.GoodsService;
import com.kc.bus.service.InportService;
import com.kc.bus.service.OutportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kc.bus.service.ProviderService;
import com.kc.bus.vo.InportVo;
import com.kc.bus.vo.OutportVo;
import com.kc.sys.common.DataGridView;
import com.kc.sys.domain.User;
import com.kc.sys.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老杜
 * @since 2019-11-04
 */
@Service
@Transactional
public class OutportServiceImpl extends ServiceImpl<OutportMapper, Outport> implements OutportService {

    @Autowired
    private InportMapper inportMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void addOutport(Integer id, Integer number, String remark) {
        //1.根据进货单Id查询进货单
        Inport inport = inportMapper.selectById(id);
        //2.根据进货单中的商品Id查询商品
        Goods goods = goodsMapper.selectById(inport.getGoodsid());
        //3.库存减去退货单中的退货数量
        goods.setNumber(goods.getNumber() - number);
        //4.更新库存信息
        goodsMapper.updateById(goods);
        //5.添加退货信息到退货单
        Outport outport = new Outport();
        outport.setNumber(number);
        outport.setGoodsid(goods.getId());
        outport.setPaytype(inport.getPaytype());
        outport.setProviderid(goods.getProviderid());
        outport.setRemark(remark);
        User user = (User) WebUtils.getSession().getAttribute("user");
        outport.setOperateperson(user.getName());
        outport.setOutportprice(inport.getInportprice());
        outport.setOutputtime(new Date());
        this.getBaseMapper().insert(outport);
    }

}
